//重载使用
function loadTab(tabDiv, datas) {
	jQuery("#" + tabDiv).jqGrid('setGridParam', {
		datatype: 'json',
		postData: datas
	}).trigger("reloadGrid");
}

//首载使用
function loadData(uriPrefix, tabDiv, datas, cols, models) {
	if (datas.sortorder === undefined) {
		datas.sortorder = 'DESC';
	}
	var hv = $(document).height() - 121;
	$.jgrid.defaults.styleUI = 'Bootstrap';
	$("#" + tabDiv).jqGrid({
		sortorder: datas.sortorder,
		sortname: datas.sortname,
		postData: datas,
		url: uriPrefix + '/list',
		datatype: 'json',
		mtype: 'POST',
		rownumbers: true,
		autowidth: true,
		altRows: true,
		shrinkToFit: true,
		rowNum: 20,
		rowList: [20, 50, 100],
		colNames: cols,
		colModel: models,
		pager: '#' + tabDiv + '_page',
		viewrecords: true,
		hidegrid: false,
		multiselect: true,
		height: hv,
		editurl: uriPrefix + '/crud',
		subGrid: true,
		subGridOptions: {
		    "plusicon"  : "fa fa-plus",
            "minusicon" : "fa fa-minus",
            "openicon"  : "fa fa-terminal",
            //"expandOnLoad" : true,
			"reloadOnExpand": true,
			//"selectOnExpand": true
		},
		subGridRowExpanded: function(subgrid_id, row_id) {
			var subgrid_table_id = subgrid_id + "_t";
			var pager_id = subgrid_table_id + "_page";
			$("#" + subgrid_id).html("<table id='" + subgrid_table_id + "' style='width:100%;overflow:auto'></table><div id='" + pager_id + "' class='alert-info text-center'></div>");
			$("#" + subgrid_table_id).jqGrid({
		        sortorder: datas.sortorder,
		        sortname: datas.sortname,
				url: uriPrefix + '/list?resPid='+row_id,
				editurl: uriPrefix + '/crud?resPid='+row_id,
				datatype: 'json',
				mtype: 'POST',
				rowNum: 10,
				rowList: [10, 20, 50],
				colNames: cols,
				colModel: models,
				pager: pager_id,
				multiselect: true,
				rownumbers: true,
				autowidth: true,
				height: '100%'
			});
			//显示序号
            $("#" + subgrid_table_id).jqGrid('setLabel', 'rn', 'No', {'text-align': 'left'},'');
            //crud
            setCrud(subgrid_table_id);
		}
	});

    //行号
    $("#" + tabDiv).jqGrid('setLabel', 'rn', 'No', {'text-align': 'left'},'');
	//crud
    setCrud(tabDiv);

	//窗体改变了，改变表格宽度
	$(window).bind('resize', function() {
		window.location.reload();
	});
}

function setCrud(id){
	$("#" + id).jqGrid("navGrid", "#" + id + "_page", {
		edit: true,
		add: true,
		del: true,
		search: true
	},
	{
		closeAfterEdit: true,
		reloadAfterSubmit: true,
	},
	{
		closeAfterAdd: true,
		reloadAfterSubmit: true,
		afterComplete: function(a, data, c) {
			var res = a.responseText;
			if ("ok" != res) {
				alert(res);
			}
		}
	},
	{
		closeAfterDel: true,
		reloadAfterSubmit: true
	},
	{});
}